package com.google.code.swingonfire.showcase.utils;

import static com.google.code.swingonfire.builder.JComponentBuilders.*;

import java.util.EventListener;

import javax.swing.JSlider;
import javax.swing.event.ChangeListener;

import com.google.code.swingonfire.builder.JSliderBuilder;

public class IntSliderParameterDefinition extends AbstractParameterDefinition<JSlider, Integer>
{

    private final int minimum;
    private final int value;
    private final int maximum;
    private final int majorTicks;
    private final int minorTicks;

    public IntSliderParameterDefinition(String name, int minimum, int value, int maximum, int majorTicks, int minorTicks)
    {
        super(name, Integer.class);

        this.minimum = minimum;
        this.value = value;
        this.maximum = maximum;
        this.majorTicks = majorTicks;
        this.minorTicks = minorTicks;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public JSlider createComponent(EventListener eventListener)
    {
        JSliderBuilder builder = sliderBuilder().minimum(minimum).maximum(maximum).value(value);

        for (int i = minimum; i <= maximum; i += majorTicks)
        {
            builder.label((i), format(i));
        }

        builder.majorTickSpacing(majorTicks).minorTickSpacing(minorTicks).snapToTicks(true).opaque(false);

        JSlider component = builder.build();

        component.addChangeListener((ChangeListener) eventListener);

        return component;
    }

    protected String format(int i)
    {
        return String.format("%d", i);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer getValue(JSlider component)
    {
        return component.getValue();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setValue(JSlider component, Integer value)
    {
        component.setValue(value);
    }

}
